package listbyorder.access001_100.test97;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/6 10:17
 */
public class Solution2 {

    // 方法二： 动态规划
    public boolean isInterleave(String s1, String s2, String s3) {
        if (s1.equals("")) return s2.equals(s3);
        if (s2.equals("")) return s1.equals(s3);
        int len1 = s1.length();
        int len2 = s2.length();
        if (len1 + len2 != s3.length()) return false;
        boolean[][] dp = new boolean[len1 + 1][len2 + 1];
        for (int i = 0; i <= len1; i++) {
            for (int j = 0; j <= len2; j++) {
                if (i == 0 && j == 0) {
                    dp[i][j] = true;
                } else if (i == 0) {
                    dp[i][j] = s2.substring(0, j).equals(s3.substring(0, i + j));
                } else if (j == 0) {
                    dp[i][j] = s1.substring(0, i).equals(s3.substring(0, i + j));
                } else {
                    dp[i][j] = (s1.charAt(i - 1) == s3.charAt(i + j - 1) && dp[i - 1][j]) || (s2.charAt(j - 1) == s3.charAt(i + j - 1) && dp[i][j - 1]);
                }
            }
        }
        return dp[len1][len2];
    }
}
